package com.wanchuang.shengfubao.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.help.GlideHelper;
import com.wanchuang.shengfubao.proto.Recharge;
import com.wanchuang.shengfubao.view.pinnedheader.PinnedHeaderAdapter;

import java.util.List;

public class ZhangDanAdapter extends PinnedHeaderAdapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_ITEM_TIME = 0;
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;


    private List<ZhangDanBean> mDataList;
    private OnTimeClickListener onTimeClickListener;

    public ZhangDanAdapter() {
        this(null, null);
    }

    public ZhangDanAdapter(List<ZhangDanBean> dataList, OnTimeClickListener onTimeClickListener) {
        mDataList = dataList;
        this.onTimeClickListener = onTimeClickListener;
    }

    public void setData(List<ZhangDanBean> dataList) {
        mDataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM_TIME) {
            return new TitleHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_linear_title, parent, false));
        } else {
            return new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_linear_content, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_ITEM_TIME) {
            TitleHolder titleHolder = (TitleHolder) holder;
            Recharge.EarningsStatics title = mDataList.get(position).getTitle();
            titleHolder.tvSelectTime.setText(title.getYmdDate().substring(title.getYmdDate().length() - 2));
            titleHolder.tvSelectTime.setOnClickListener(v -> {
                String year = title.getYmdDate().substring(0, 4);
                String month = title.getYmdDate().substring(4, 6);
                String day = title.getYmdDate().substring(title.getYmdDate().length() - 2);
                onTimeClickListener.onClick(v, year, month, day);
            });
            titleHolder.tvBiShun.setText(title.getNum());
            titleHolder.tvTotalMoney.setText("￥" + title.getAmount());
        } else {
            ContentHolder contentHolder = (ContentHolder) holder;
            contentHolder.tvContentName.setText(mDataList.get(position).getContent().getLgType());
            contentHolder.tvCountMoney.setText(mDataList.get(position).getContent().getLgDesc());
            contentHolder.tvTime.setText(mDataList.get(position).getContent().getLgAddtime());
            contentHolder.tvMoney.setText(mDataList.get(position).getContent().getLgAvAmount());
            GlideHelper.INSTANCE.loadImage(contentHolder.ivImage, mDataList.get(position).getContent().getLogo());
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mDataList.get(position).getType() == 1) {
            return VIEW_TYPE_ITEM_TIME;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }

    @Override
    public boolean isPinnedPosition(int position) {
        return getItemViewType(position) == VIEW_TYPE_ITEM_TIME;
    }

    static class ContentHolder extends RecyclerView.ViewHolder {

        ImageView ivImage;
        TextView tvContentName;
        TextView tvCountMoney;
        TextView tvTime;
        TextView tvMoney;

        ContentHolder(View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_image);
            tvContentName = itemView.findViewById(R.id.tv_content_name);
            tvCountMoney = itemView.findViewById(R.id.tv_count_money);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvMoney = itemView.findViewById(R.id.tv_money);
        }
    }

    static class TitleHolder extends RecyclerView.ViewHolder {

        TextView tvSelectTime;
        TextView tvBiShun;
        TextView tvTotalMoney;

        TitleHolder(View itemView) {
            super(itemView);
            tvSelectTime = itemView.findViewById(R.id.tv_select_time);
            tvBiShun = itemView.findViewById(R.id.tv_bi_shun);
            tvTotalMoney = itemView.findViewById(R.id.tv_total_money);
        }
    }

    /**
     * 监听输入框内容
     */
    public interface OnTimeClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, String year, String month, String day);
    }

}
